package org.shop.controller.app.live;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.Recruitment;
import org.shop.entity.security.MyUserDetail;
import org.shop.service.RecruitmentService;
import org.shop.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RecruitmentController {

	private static Logger logger = LogManager.getLogger(RecruitmentController.class);
	
	@Autowired
	private RecruitmentService recruitmentService;
	
	
	/**
	 * 获取招聘信息列表
	 * @param req
	 * @return
	 */
	@RequestMapping("app/getRecruitmentList")
	@ResponseBody
	public List<Recruitment> getRecruitmentList(HttpServletRequest req){
		Map<String,Object> params = new HashMap<String,Object>();
		List<Recruitment> rmList = null;
		String currentPage = req.getParameter("currentPage");
		String pageSize = req.getParameter("pageSize");
		String jobName = req.getParameter("jobName");
		try {
			params.put("currentPage", (Integer.parseInt(currentPage) - 1) * Integer.parseInt(pageSize));
    		params.put("pageSize", pageSize);
    		params.put("jobName", jobName);
    		rmList = recruitmentService.getRecruitmentList(params);
		}catch(Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return rmList;
	}
	
	
	/**
	 * 跳转招聘详情
	 * @param req
	 * @return
	 */
	@RequestMapping("app/toJobDetails")
	public String toJobDetails(HttpServletRequest req) {
		Long recruitmentId = Long.parseLong(req.getParameter("recruitmentId"));
		try {
			Recruitment selectByPrimaryKey = recruitmentService.selectByPrimaryKey(recruitmentId);
			req.setAttribute("recruitment", selectByPrimaryKey);
		}catch(Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			req.setAttribute("recruitment", null);
		}
		return "app/find/job_details";
	}
	
	/**
	 * 发布工作
	 * @param req
	 * @return
	 */
	@RequestMapping("app/addJobInfo")
	@ResponseBody
	public Map<String,Object> addJobInfo(HttpServletRequest req) {
		Map<String,Object> map = new HashMap<String,Object>();
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String jobName = req.getParameter("jobName");
		String company = req.getParameter("company");
		String salary = req.getParameter("salary");
		String settlementWay = req.getParameter("settlementWay");
		String wordTime = req.getParameter("wordTime");
		String workPlace = req.getParameter("workPlace");
		String workDetails = req.getParameter("workDetails");
		String workRequirements = req.getParameter("workRequirements");
		String userPhone = req.getParameter("userPhone");
		String textBlackList=CommonUtil.getPropertiesParam("textBlackList");
        for(String textBlack:textBlackList.split(",")){
            if(workDetails.contains(textBlack)){
                map.put("state", 2);
                map.put("msg", "工作详情不合法~");
                return map;
            }
            if(workRequirements.contains(textBlack)){
            	map.put("state", 2);
            	map.put("msg", "职位要求不合法~");
            	return map;
            }
        }
		try {
			Recruitment rt = new Recruitment();
			rt.setJobName(jobName);
			rt.setCompany(company);
			rt.setSalary(Integer.parseInt(salary));
			rt.setSettlementWay((byte) Integer.parseInt(settlementWay));
			rt.setWordTime(wordTime);
			rt.setWorkPlace(workPlace);
			rt.setWorkDetails(workDetails);
			rt.setWorkRequirements(workRequirements);
			rt.setUserPhone(userPhone);
			rt.setUserId(ud.getUserId());
			rt.setUserName(ud.getUsername());
			rt.setUserHeadImg(ud.getUserPhoto());
			int num = recruitmentService.insertSelective(rt );
			map.put("num", num);
			map.put("msg", "发布职位成功~");
		}catch(Exception e) {
			map.put("num", 0);
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return map;
	}
	
}
